﻿;

var Sample = {
    queryString:'#ajax-post-query-string',
    jsonObject: '#ajax-post-json',
    jsonString: '#ajax-post-json-string',
    objectArray: '#ajax-post-object-array',

    init: function() {
        Sample.registerEvents();
    },

    registerEvents: function() {
        Sample.registerTooltip();

        var listButton = [];
        listButton.push({ id: Sample.queryString, func: Sample.AjaxSingleTypes3 });
        listButton.push({ id: Sample.jsonString, func: Sample.AjaxSingleTypes });
        listButton.push({ id: Sample.jsonObject, func: Sample.AjaxSingleTypes2 });
        listButton.push({ id: Sample.objectArray, func: Sample.AjaxSingleTypes4 });
        listButton.push({ id: '#ajax-post-object-type', func: Sample.AjaxObjectType });
        listButton.push({ id: '#ajax-post-object-type2', func: Sample.AjaxObjectType2 });
        listButton.push({ id: '#ajax-post-object-types', func: Sample.AjaxObjectTypes });
        listButton.push({ id: '#ajax-post-object-types2', func: Sample.AjaxObjectTypes2 });
        listButton.push({ id: '#ajax-post-mix-types', func: Sample.AjaxMixTypes });
        $.each(listButton, function(key, value) {
            $(value.id).off('click').on('click', function() {
                value.func();
            });
        });
    },

    registerTooltip: function() {
        $('a').qtip({
            // Grab some elements to apply the tooltip to
            content: {
                text: function(event, api) {
                    $.ajax({
                        url: '/Home/Tooltip' //api.elements.target.attr('data-tooltip-url')
                    }).then(function(data) {
                        var content = data;
                        //if (CsmHelper.isNull(content)) {
                        //    content = ' ';
                        //}
                        //set content for tooltip
                        api.set('content.text', content);
                        //set title for tooltip
                        //api.set('content.title', data.ClauseName);
                    }, function(xhr, status, error) {
                        api.set('content.text', status + ': ' + error);
                    });
                    return 'Loading...';
                }
            }
        });
    },

    AjaxSingleTypes: function () {
        $.ajax({
            url: '/Home/AjaxSingleTypes',
            type: 'POST',
            //data: {
            //    Address1: "423 Judy Road",
            //    Address2: "1001",
            //    City: "New York",
            //    State: "NY",
            //    ZipCode: "10301",
            //    Country: "USA"
            //},
            data: JSON.stringify({
                field1: 123,
                field2: 'abc',
            }),
            contentType: 'application/json; charset=utf-8', // must use with JSON.stringify(obj); this is post data type
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    AjaxSingleTypes2: function () {
        $.ajax({
            url: '/Home/AjaxSingleTypes',
            type: 'POST',
            data: {
                field1: 123,
                field2: 'abc',
            },
            //data: { // or use this work well, too
            //    'field1': 123,
            //    'field2': 'abc',
            //},
            //contentType: 'application/json; charset=utf-8',
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8', // default contentType, no need 
            //dataType: "json", // no need, this is return dataType
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },
    
    AjaxSingleTypes3: function () {
        var postdata = 'field1=123&field2=abc'; // similiar $(formId).serialize(); not good in case field2=abc &cd
        $.ajax({
            url: '/Home/AjaxSingleTypes',
            type: 'POST',
            data: postdata,
            //contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    AjaxSingleTypes4: function () {
        var postdata = [{ name: 'field1', value: 123 }, { name: 'field2', value: 'abc' }]; // similiar $(formId).serializeArray();
        $.ajax({
            url: '/Home/AjaxSingleTypes',
            type: 'POST',
            data: postdata,
            //contentType: 'application/json; charset=utf-8',
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    //
    AjaxObjectType: function () {
        var postdata = {
            field1: 123,
            field2: 'abc',
            Content: 'this is content'
        };
        $.ajax({
            url: '/Home/AjaxObjectType',
            type: 'POST',
            data: postdata,
            
            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    AjaxObjectType2: function () {
        var postdata = {
            'obj1.field1': 123,
            'obj1.field2': 'abc',
            'Content': 'this is content'
        };
        $.ajax({
            url: '/Home/AjaxObjectType',
            type: 'POST',
            data: postdata,

            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    AjaxObjectTypes: function () {
        var postdata = {
            field1: 123, // be careful with field name that is the same in other model
            field2: 'abc',
            Content: 'this is content'
        };
        $.ajax({
            url: '/Home/AjaxObjectTypes',
            type: 'POST',
            data: postdata,

            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    AjaxObjectTypes2: function () {
        var postdata = {
            'obj1.field1': 123,
            'obj1.field2': 'abc',
            'Content': 'this is content'
        };
        $.ajax({
            url: '/Home/AjaxObjectTypes',
            type: 'POST',
            data: postdata,

            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },

    AjaxMixTypes: function () {
        var postdata = {
            'obj1.field1': 123,
            'obj1.field2': 'abc&d',
            'text': 'this is content'
        };
        $.ajax({
            url: '/Home/AjaxMixTypes',
            type: 'POST',
            data: postdata,

            success: function (data) {
                alert(data.success);
            },
            error: function () {
                alert("error");
            }
        });
    },
};